package com.cqupt.command.imp;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.cqupt.command.Command;
import com.cqupt.utils.FileReadMan;

public class WcCommand implements Command {
	// 选项列表
	private List<String> options;
	// 参数列表
	private String parameters;
	// 上命令的结果
	private String result;

	public WcCommand(String command, String result) {
		// 命令解析得到选项
		this.options = getOptions(command);
		// 命令解析得到参数
		this.parameters = getParameters(command);
		// 得到上个命令的结果
		this.result = result;
	}

	private List<String> getOptions(String command) {
		List<String> result = new ArrayList<>();
		Pattern pattern = Pattern.compile("-[a-z,A-Z]+");
		Matcher matcher = pattern.matcher(command);
		while (matcher.find()) {
			String parameterA = matcher.group();
			char[] chars = parameterA.substring(1, parameterA.length()).toCharArray();
			for (int i = 0; i < chars.length; i++) {
				result.add(chars[i] + "");
			}
		}
		return result;
	}

	private String getParameters(String command) {
		String[] cmd = command.split(" ");
		return cmd[cmd.length - 1].trim();
	}

	// -l返回行数，-w返回字数，-c返回字节数，默认返回是三种
	@Override
	public String run() {
		// 获取数据源
		List<String> listString = new ArrayList<>();
		if (result != null) {
			String[] resultSource = result.split("\r?\n");
			for (int i = 0; i < resultSource.length; i++) {
				String sourceLine = resultSource[i];
				if (!"".equals(sourceLine)) {
					listString.add(sourceLine.trim());
				}
			}
		} else {
			FileReadMan fileReadMan = new FileReadMan(parameters);
			while (fileReadMan.hasNext()) {
				String sourceLine = fileReadMan.next();
				if (!"".equals(sourceLine)) {
					listString.add(sourceLine.trim());
				}
			}
		}
		// 获得行数
		int lines = getLines(listString);
		// 获得单词数，只统计英文
		int words = getWords(listString);
		// 获取字节数
		int bytes = getBytes(listString);
		// 确定选项
		String option = "";
		if (options.size()!=0) {
			option = options.get(0);
		}
		switch (option) {
		case "l":
			return lines + "\t" + parameters;
		case "w":
			return words + "\t" + parameters;
		case "c":
			return bytes + "\t" + parameters;
		default:
			return lines + "\t" + words + "\t" + bytes + "\t" + parameters;
		}
	}

	private int getBytes(List<String> listString) {
		int count = 0;
		for (int i = 0; i < listString.size(); i++) {
			String line = listString.get(i);
			int length = line.getBytes().length;
			count += length;
		}
		return count;
	}

	private int getWords(List<String> listString) {
		int count = 0;
		for (int i = 0; i < listString.size(); i++) {
			String line = listString.get(i);
			String[] array = { ".", " ", "?", "!" };
			for (int j = 0; j < array.length; j++) {
				line = line.replace(array[j], ",");
			}
			String[] wordsTmp = line.split(",");
			for (int j = 0; j < wordsTmp.length; j++) {
				if (wordsTmp[j].trim().matches("[a-z,A-Z]+")) {
					count++;
				}
			}
		}
		return count;
	}

	private int getLines(List<String> listString) {
		return listString.size();
	}

}
